<?php
class Tests_CrudModel extends grocery_CRUD_Model
{
    function get_list()
    {
    	if($this->table_name === null)
    		return false;
    	
		$select = "teachers.user_id, teachers.id, teachings.subject_id, teachings.class_id";
		$this->db->select($select, false);
		$this->db->where('subject_id',$_SESSION['subject_id']);
		$this->db->where('teachers.user_id',$_SESSION['id']);
		$this->db->join('teachings','teachings.teacher_id = teachers.id');
		$this->db->join('users','users.id = teachers.user_id');
    	$rs = $this->db->get('teachers')->result_array();
		//echo var_dump($rs);
		
		
		
    	$select = "{$this->table_name}.*";
    	
		// ADD YOUR SELECT FROM JOIN HERE <------------------------------------------------------
		// for example $select .= ", user_log.created_date, user_log.update_date";
		//$select .= ", users.username, users.password";
		
    	if(!empty($this->relation))
    		foreach($this->relation as $relation)
    		{
    			list($field_name , $related_table , $related_field_title) = $relation;
    			$unique_join_name = $this->_unique_join_name($field_name);
    			$unique_field_name = $this->_unique_field_name($field_name);
    			
				if(strstr($related_field_title,'{'))
    				$select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name";
    			else    			
    				$select .= ", $unique_join_name.$related_field_title as $unique_field_name";
    			
    			if($this->field_exists($related_field_title))
    				$select .= ", {$this->table_name}.$related_field_title as '{$this->table_name}.$related_field_title'";
    		}
    		
    	$this->db->select($select, false);
    	
		// ADD YOUR JOIN HERE for example: <------------------------------------------------------
		// $this->db->join('user_log','user_log.user_id = users.id');
		//$this->db->join('users','users.id = '. $this->table_name . '.user_id');
		$pertama = true;
		$this->db->where('subject_id',$_SESSION['subject_id']);
		foreach($rs as $row)
			if($pertama){			
				$this->db->where('class_id',$row["class_id"]);
				$pertama = false;
			}
			else
				$this->db->or_where('class_id',$row["class_id"]);
			
		//$this->db->where('teachers.user_id',$row['class_id']);
		//$this->db->where('class_id',$rs[0]['class_id']);
    	$results = $this->db->get($this->table_name)->result();
    	return $results;
    }
	
	function get_edit_values($primary_key_value)
    {
    	$primary_key_field = $this->get_primary_key();
    	$this->db->where($primary_key_field,$primary_key_value);
    	$result = $this->db->get($this->table_name)->row();
    	return $result;
    }

	function db_update($post_array, $primary_key_value)
    {
		$primary_key_field = $this->get_primary_key();
		$CI =& get_instance();
		$CI->load->model('test_question_model','test_question');
		
		$CI->load->model('tests_model', 'tests', true);
		if($post_array['start']==true)
			$id = $this->tests->start_duration($primary_key_value, $post_array['duration']);
		else
			$id = $this->tests->stop_duration($primary_key_value);
		
		$rs = $CI->test_question->insert($primary_key_value, $post_array['question_id'], $post_array['percentage'], $post_array['total_qty']);
    	return $this->db->update($this->table_name,$post_array, array( $primary_key_field => $primary_key_value));
    }

	function db_insert($post_array)
    {
		$CI =& get_instance();
		$CI->load->model('school_years_model','school_years');
		$CI->load->model('test_question_model','test_question');
		
		$rs = $CI->school_years->get_active_school_year();
		$post_array['school_year_id'] = $rs[0]['id'];
		$post_array['date'] = date('Y-m-d');
		$post_array['time'] = date('H:i:s');
		$post_array['subject_id'] = $_SESSION['subject_id'];
		$post_array['ip'] = $_SERVER['REMOTE_ADDR'];
		
		$insert = $this->db->insert($this->table_name,$post_array);
		
    	if($insert)
    	{
			$last_test_id = $this->db->insert_id();
			$rs = $CI->test_question->insert($last_test_id, $post_array['question_id'], $post_array['percentage'], $post_array['total_qty']);
    		$CI->load->model('tests_model', 'tests', true);
			if($post_array['start']==true)
				$id = $this->tests->start_duration($last_test_id, $post_array['duration']);
			else
				$id = $this->tests->stop_duration($last_test_id);
				
			return $rs;
    	}
		return false;
    }
	
	function db_delete($primary_key_value)
    {
    	$primary_key_field = $this->get_primary_key();
    	
    	if($primary_key_field === false)
    		return false;
    	
    	$this->db->limit(1);
    	$this->db->delete($this->table_name,array( $primary_key_field => $primary_key_value));
    	if( $this->db->affected_rows() != 1)
    		return false;
    	else{
			$data['test_id'] = $primary_key_value;
			$this->db->delete('test_question', $data);
    		return true;
		}
    }
	
}